<?php
namespace Project_725\Lib;

use Project_725\Model as Model;

use Zend\Mail\Message;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Mime;
use Zend\Mail\Transport\Smtp;
use Zend\Mail\Transport\SmtpOptions;
use TwinPeaks\Core as Core;

use Project_725\View\Mailer as View;

class Mailer extends Core\CStdlib
{
	use Core\TSingleton;

	/**
	 * @var \Zend\Mail\Message
	 */
	private $message;
	
	/**
	 * @var \Zend\Mail\Transport\Sendmail
	 */
	private $mailer;
	
	/**
	 * @var \TwinPeaks\Core\CLayout
	 */
	protected $layout;
	
	/**
	 * @return void
	 */
	protected function onCreate()
	{
		$config = self::getApplication()->getConfig();
		$this->message = new Message;
		$this->message->setFrom('admin@geomania.mygbiz.com');
		$this->mailer = new Smtp();
		$options   = new SmtpOptions(array(
			'name' => 'Gmail',
			'host'              => 'smtp.gmail.com',
			'connection_class'  => 'login',			
			'port' => 587,
			'connection_config' => array(
				'ssl'       => 'tls',
				'username' => $config->mailer_notification,
				'password' => $config->mailer_notification_pass,
			)
		));
		
		$this->mailer->setOptions($options);
		$this->layout = View\Layout::getInstance();
	}
	
	/**
	 * @return 
	 */
	protected function createMimeMessage($htmlText)
	{
		$message = new MimeMessage;		
		$html = new \Zend\Mime\Part($htmlText);
		$html->type = Mime::TYPE_HTML;
		$html->disposition = Mime::DISPOSITION_INLINE;
		$html->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
		$html->charset = 'utf-8';
		
		$message->addPart($html);
		return $message;
	}
	
	
	/**
	 * @retun TwinPeaks\Core\CContext
	 */
	protected function getCurrentCtx()
	{
		return $this->layout->getTemplateById(View\Layout::MESSAGE_TID)
			->getContent()->getContext();
	}
	
	/**
	 * @return void
	 */
	function processInvitation($email, $defaultPass, $invite)
	{
		$ctx = $this->getCurrentCtx();		
		$ctx->email = $email;
		$ctx->pass = $defaultPass;
		$ctx->sender_name = $invite->getSenderName();
		$ctx->recipient_name =$invite->getRecipientName();
		$ctx->app_download_url = URLManager::getInstance()->getDownloadApp($invite->getCode());
		
		$subject = 'Invitation from ' . $invite->getSenderName();
		$this->message->setBody($this->createMimeMessage($this->layout->render()));		
		
		$this->message->setSubject($subject);
		$this->message->setTo($email);
		$this->mailer->send($this->message);
	}
	
	/**
	 * @return void
	 */
	function processInviteWithdrawal($invite)
	{
		$ctx = $this->getCurrentCtx();
		$ctx->sender_name = $invite->getSenderName();
		$ctx->sent_at = $invite->getSentAt();
		
		$subject = 'Project_725 - invitation widthdrawal';
		$this->message->setBody($this->createMimeMessage($this->layout->render()));		
		
		$recipient = Model\User::getById($invite->getRecipientId());
		
		$this->message->setSubject($subject);
		$this->message->setTo($recipient->getEmail());
		$this->mailer->send($this->message);
		
	}
	
	/**
	 * @return void
	 */
	private function send($email = null)
	{
		if ($email)
			$this->message->setTo($email);
		$this->message->setBody($this->createMimeMessage($this->layout->render()));		
		$this->mailer->send($this->message);
	}
	
	/**
	 * @return void
	 */
	function processSignup($email, $pass, $code)
	{
		$ctx = $this->getCurrentCtx();
		
		$ctx->login = $email;
		$ctx->pass = $pass;
		$ctx->email_confirm_url = URLManager::getInstance()->getEmailConfirm($code);
		
		$this->message->setSubject('Signup confirmation');
		$this->send($email);
	}
}
